%% Analyze the match score for the different stimuli over time
% either on match trials (match_trials == true), or on non-match trials
% (match_trials = false).
% 
function averages = gen_ca_match_score_decision_area(match_trials)


seed = 0;
averages = single_subject(seed, match_trials)
i=2;
for seed = 100:100:400
    averages(:,:,i) = single_subject(seed, match_trials);
    i=i+1;
end
pooled_averages = mean(averages,3);

pooled_averages(2:4,:)=pooled_averages(4:-1:2,:); % reverse order from -90 to -270.

pool_length_in_bins = 40;

close all

figure(2);

% Plot over the single object plot.
hold off;
plot(pooled_averages(1,:),'y','LineWidth',3);
hold on;
plot(pooled_averages(2,:),'g','LineWidth',3);
plot(pooled_averages(3,:),'r','LineWidth',3);
plot(pooled_averages(4,:),'c','LineWidth',3);

ylabel('Match score','FontSize',16,'Color','w')
xlabel('T (sec)','FontSize',16,'Color','w')

set(gca,'XTick',[1:pool_length_in_bins/4:pool_length_in_bins],'FontSize',14,'XColor',[1 1 1])
set(gca,'XTickLabel',{'3','3.5','4','4.5','5'},'XColor',[1 1 1])

set(gca,'YTick',0:.2:1,'FontSize',14,'YColor','w')
%legend({'Match response', 'Mismatch'},'FontSize',14);

set(gcf, 'color', 'k');
set(gca, 'color', 'k');
set(gcf, 'InvertHardCopy', 'off');

h=legend('Stim. B angle = Stim. A angle','Stim. B ang. = A ang. - 90 deg.','Stim. B ang. = A ang. - 180 deg.','Stim. B ang. = A ang. - 270 deg.');
set(h,'Color','w');




end

%% Read data from the pwd.
function subject1_pooled_averages = single_subject(seed, match_trials)

filename = dir(['final_seed' num2str(seed,'%03d') '_phase_6*']);
cd (filename.name);



analyzeSequenceEssential('MTS','p23M',500,999, 152000, 155999, 905000, 1224999)
global PSTATES

% 64, 5-second trials.

pool_length_in_bins = 40;

% Reconstruct the start bins for the Mental rotation experiment: 64 trials
% of 5 seconds starting at the response period, 3 seconds into the trial.
startbin=3*20+1;
trial_bins = reshape(startbin:100:64*100,8,8)';

stimulus_numbers = 1:4;
object1_pool_start_bins = [];
object1_object_to_pool = [];

if match_trials
    temp = trial_bins(1:4,1:4);
else
    % nonmatch trials
    temp = trial_bins(1:4,5:8);
end
for position = 1:4
    temp2 = [];
    for row=1:4
        col = row + position -1;
        if col > 4
            col = col - 4; % wrap around.
        end
        temp2 = [temp2 temp( row, col )];
    end
    object1_pool_start_bins(position,:) = temp2(:)';

    object1_object_to_pool(position,:) = stimulus_numbers;
    
end

object1_pooled_averages = plot_georgopoulos(PSTATES, object1_pool_start_bins,...
    object1_object_to_pool , pool_length_in_bins);


% Analyze the other object now; template is taken from different window
% containing object2.
analyzeSequenceEssential('MTS','p23M',500,999,156000,159999,905000,1224999)
global PSTATES


stimulus_numbers = [1 2 3 4];
object2_pool_start_bins = [];
object2_object_to_pool = [];

if match_trials
    temp = trial_bins(5:8,5:8);
else
    % nonmatch trials
    temp = trial_bins(5:8,1:4);
end
for position = 1:4
    temp2 = [];
    for row=1:4
        col = row + position -1;
        if col > 4
            col = col - 4; % wrap around.
        end
        temp2 = [temp2 temp( row, col )];
    end
    object2_pool_start_bins(position,:) = temp2(:)';

    object2_object_to_pool(position,:) = stimulus_numbers;
    
end
object2_pooled_averages = plot_georgopoulos(PSTATES, object2_pool_start_bins,...
    object2_object_to_pool , pool_length_in_bins);


subject1_pooled_averages = (object1_pooled_averages + object2_pooled_averages)*0.5;

close all

figure(2);

% Plot over the single object plot.
hold off;
plot(subject1_pooled_averages(1,:),'y','LineWidth',3);
hold on;
plot(subject1_pooled_averages(2,:),'g','LineWidth',3);
plot(subject1_pooled_averages(3,:),'r','LineWidth',3);
plot(subject1_pooled_averages(4,:),'c','LineWidth',3);

ylabel('Match strength','FontSize',16,'Color','w')
xlabel('T (msec)','FontSize',16,'Color','w')

set(gca,'XTick',[1:pool_length_in_bins/4:pool_length_in_bins],'FontSize',14,'XColor',[1 1 1])
set(gca,'XTickLabel',{'0','500','1000','1500','2000'},'XColor',[1 1 1])

set(gca,'YTick',0:.2:1,'FontSize',14,'YColor','w')
%legend({'Match response', 'Mismatch'},'FontSize',14);

set(gcf, 'color', 'k');
set(gca, 'color', 'k');
set(gcf, 'InvertHardCopy', 'off');

h=legend('Stimulus B angle','Stim. B angle + 90 deg.','Stim. B angle + 180 deg.','Stim. B angle + 270 deg.');
set(h,'Color','w');

cd ..


end

